package com.inspur.gs.sbssdl.dzyhpgl.dzyhphz.dzyhphz.front.sbssdzyhzrdetail_frm.voactions;
import com.inspur.edp.bef.api.lcp.ILcpFactory;
import com.inspur.edp.bef.api.lcp.IStandardLcp;
import com.inspur.edp.bff.spi.*;
import com.inspur.edp.bef.api.action.*;
import com.inspur.edp.cef.entity.entity.IEntityData;
import com.inspur.edp.cef.entity.entity.IEntityDataCollection;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public class checkSlVOAction extends AbstractFSAction<VoidActionResult> {

	private String id;

	private String hzdId = "c421dde8-8ab3-41f3-b92b-97d5e2184ebc";

	private String kcId = "65360fb1-9e73-426e-80d4-f31bfef503c8";

	public checkSlVOAction(String id) {
		this.id = id;
	}

	/**
	 * 1.检查子项目的申请数量是否大于剩余数量<br>
	 *   如果大于,退回<br>
	 * 2.如果小于等于，扣减 <b>剩余数量</b> 字段<br>
	 * 如果审批通过了再扣减 <b>剩余</b> 字段<br>
	 * 如果审批取消,增加  <b>剩余数量</b> 字段<br>
	 */
	@Override
	public void execute() {

		ILcpFactory lcpFactory = SpringBeanUtils.getBean(ILcpFactory.class);
		IStandardLcp hzdLcp = lcpFactory.createLcpByBEId(hzdId);

		IEntityData item = hzdLcp.retrieve(id).getData();
		IEntityDataCollection hzdMx = item.getChilds().get("hzdMx");

		List<String> errorList = new ArrayList<String>();
		for(IEntityData mx : hzdMx){
			String kcName = checkSl(mx);
			if (kcName != null){
				errorList.add(kcName);
			}
		}

		if (errorList.size() > 0){
			String msg = String.join("<br>", errorList);
			throw new RuntimeException("申请数量大于可转出数量,请重新申请");
		}

	}


	/**
	 * 检查数量,不合格则返回名称
	 * 格式 [物料名称]-[使用人员] 可转出数量:[剩余数量]
	 * @param mx
	 * @return
	 */
	private String checkSl(IEntityData mx){

		return null;
	}
}
